$(function () {
    merchant = {
        issueCoupons: function (that) {
            var ids = getIds();
            if (!ids) {
                notify.warning("请选择要发放优惠券的商家");
                return false;
            }
            var json = JSON.parse(that.attr('data-json'));
            var url = json.url + '?ids=' + ids.join(',');
            zhiju_show('发放优惠券', url, json.width, json.height, json.full);
        }
    };

    buttons = {
        preview: function (id, url) {
            zhiju_show('查看', url + '?id=' + id, 900);
        },
        add: function (that) {
            var json = JSON.parse(that.attr('data-json'));
            if (json.width) {
                zhiju_show(json.unity_title, json.href + '&tableUniqueTitle=' + json.unity_title + '&openShowType=1', json.width, json.height, json.full);
            } else {
                top.layui.index.openTabsPage(json.href + "&back=" + json.back_url + '&tableUniqueTitle=' + json.unity_title, json.unity_title)
            }
        },
        goBack: function (url, title) {
            parent.layui.admin.events.closeThisTabs();
            top.layui.index.openTabsPage(url, title)
        },
        edit: function (data, tool) {
            if (tool.width) {
                var url = tool.url + '&id=' + data.id + '&uid=' + data.uid + '&tableUniqueTitle=' + tool.unity_title;
                zhiju_show(tool.unity_title, url + '&openShowType=1', tool.width, tool.height, tool.full);
            } else {
                var url = tool.url + '&id=' + data.id + '&uid=' + data.uid + "&back=" + tool.back_url + '&tableUniqueTitle=' + tool.unity_title
                top.layui.index.openTabsPage(url, tool.unity_title)
            }
        },
        save: function (id, key, val, url) {
            $.ajax({
                type: 'POST',
                url: url,
                data: {
                    id: id,
                    key: key,
                    val: val,
                    tableUniqueStr: $("#tableUniqueStr").val(),
                    tableUniqueTitle: $("#tableUniqueTitle").val()
                },
                dataType: "json",
                success: function (data) {
                    if (data.code != 1) {
                        layer.msg(data.msg);
                    }
                }
            });
        },
        remove: function (tool, data, obj) {
            console.log(tool);
            layer.confirm('确定要删除吗？', {title: '请确认'}, function (index) {
                layer.load();
                $.ajax({
                    type: 'POST',
                    url: tool.url,
                    data: {id: data.id, tableUniqueTitle: tool.unity_title},
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 1) {
                            notify.success(data.msg);
                            obj.del();
                        } else {
                            notify.error(data.msg);
                        }
                    }
                });
                layer.closeAll();
            });
        },
        removeAll: function (that, type = 1) {
            //var url = that.attr('data-url');
            var json = JSON.parse(that.attr('data-json'));
            var url = json.href;
            var ids;
            if (type == 2) {
                ids = tableCheck.getData();
            } else {
                ids = getIds();
            }
            if (!ids) {
                notify.warning("请选择要删除的数据");
                return false;
            }
            layer.confirm('确定要删除所选数据吗？', {title: '请确认'}, function (index) {
                layer.load();
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: {id: ids},
                    dataType: "json",
                    success: function (data) {
                        layer.closeAll();
                        if (data.code == 1) {
                            notify.success("操作成功", 1500, function () {
                                location.reload();
                            });
                        } else {
                            notify.error(data.msg);
                        }
                    }
                });

            });


        },
        //重置密码
        reset: function (that, type) {
            var ids = getIds();
            var json = JSON.parse(that.attr('data-json'));
            if (!ids) {
                notify.warning("请选择要重置密码的数据");
                return false;
            }
            notify.loading("加载中");
            $.ajax({
                type: 'POST',
                url: json.url + '?type=' + type,
                data: {ids: ids},
                dataType: "json",
                success: function (data) {
                    notify.destroyAll()
                    notify.success(data.msg);
                }
            });

        },
        batchAudit: function (that) {
            var ids = getIds();
            if (!ids) {
                notify.warning("请选择要批量操作的数据");
                return false;
            }
            var json = JSON.parse(that.attr('data-json'));

            layer.confirm('确定要批量操作吗？', {title: '请确认'}, function (index) {
                layer.load();
                $.ajax({
                    type: 'POST',
                    url: json.url,
                    data: {ids: ids},
                    dataType: "json",
                    success: function (data) {
                        layer.closeAll();
                        if (data.code == 1) {
                            notify.success(data.message ? data.message : data.msg, 1500, function () {
                                location.reload();
                            });
                        } else {
                            notify.error(data.message ? data.message : data.msg);
                        }
                    }
                });
            });
        },
        exportExcel: function (that) {
            layerLoad();
            $.ajax({
                url: that.attr('data-url')
                , data: form.val("search-form")
                , dataType: 'json'
                , success: function (res) {
                    var timestart = Date.now()
                    if (res.title) {
                        table.exportFile(res.title, res.data, {
                            type: 'csv', // 导出格式
                            title: that.attr('data-title')
                        });
                    } else {
                        table.exportFile('form_table', res.data, {
                            type: 'csv', // 导出格式
                            title: that.attr('data-title')
                        });
                    }
                    var timeend = Date.now()
                    var spent = (timeend - timestart) / 1000
                    layer.closeAll();
                    notify.success('已导出，耗时 ' + spent + ' s');
                }
                , error: function () {
                    layer.closeAll();
                    notify.error('获取数据失败！');
                }
            })
        }
    };
    //公共操作
    common = {
        _batchSubmit: function (that) {
            var ids = getIds();
            if (!ids) {
                notify.warning("请选择要操作的数据");
                return false;
            }
            var json = JSON.parse(that.attr('data-json'));
            layer.confirm('正在进行<span style="color: red">《' + that.text() + '》</span>操作<br/>确定要执行吗？'
                , {title: '请确认'}
                , function (index) {
                    layerLoad();
                    $.ajax({
                        type: 'POST',
                        url: json.url,
                        data: {ids: ids},
                        dataType: "json",
                        success: function (data) {
                            layer.closeAll();
                            if (data.code == 1) {
                                notify.success(data.message ? data.message : data.msg, 1500, function () {
                                    location.reload();
                                });
                            } else {
                                notify.error(data.message ? data.message : data.msg);
                            }
                        }
                    });
                });
        },
        _submit: function (url, title, data) {
            layer.confirm('确定要' + title + '吗？', {title: '请确认'}, function (index) {
                layer.load();
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: data,
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 1) {
                            notify.success(data.msg, 1500, function () {
                                location.reload();
                            });
                        } else if(data.code == 2){
                            notify.success(data.msg, 1500, function () {
                                window.open("/");
                            });
                        } else {
                            notify.error(data.msg);
                        }
                    }
                });
                layer.closeAll();
            });
        },
        _openNewPage: function (data, tool) {
            var url = (tool.url.indexOf('?') !== -1 ? tool.url + '&' : tool.url + '?') + 'id=' + data.id + "&back=" + tool.back_url
            if (tool.width) {
                zhiju_show(tool.title, url + '&openShowType=1', tool.width, tool.height, tool.full);
            } else {
                top.layui.index.openTabsPage(url, tool.title)
            }
        },
    };

    function getIds(msg) {
        var ids = [];
        var all = getTable().data;
        if (all.length <= 0) {
            return false;
        }
        for (var i = 0; i <= all.length; i++) {
            if (all[i]) {
                ids.push(all[i].id);
            }
        }
        return ids;
    }
})

